package com.hongyuan.sellfitness.ui.find.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import androidx.lifecycle.ViewModelProviders;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.databinding.ActivityPostDetailsBinding;
import com.hongyuan.sellfitness.my_view.KeyboardLayout;
import com.hongyuan.sellfitness.my_view.nine_gridimg.NineGridImageViewAdapter;
import com.hongyuan.sellfitness.ui.find.beans.PostDetailBean;
import com.hongyuan.sellfitness.ui.find.view_mode.PostDetailsViewModel;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.enitity.UserViewInfo;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class PostDetailsActivity extends CustomActivity<ActivityPostDetailsBinding, PostDetailsViewModel> implements KeyboardLayout.onKeyboaddsChangeListener{

    private String circle_id;

    @Override
    protected boolean isFullScreen() {
        return false;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_post_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public PostDetailsViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(PostDetailsViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();

        circle_id = getBundle().getString("circle_id");
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);

        setEnableLoadMore(Constants.GET_CIRCLE_REVIEWLIST);
        setEnableRefresh(Constants.GET_CIRCLE_REVIEWLIST);

        binding.comBack.setOnClickListener(view -> finish());

        binding.outBox.setOnkbdStateListener(this);

        binding.nineGridImg.setAdapter(new NineGridImageViewAdapter<PostDetailBean.CiBean>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, PostDetailBean.CiBean s) {
                if(s.getCircle_type() == 1){
                    RequestOptions options = new RequestOptions().placeholder(R.mipmap.fang_default).error(R.mipmap.fang_default).centerCrop();
                    Glide.with(getBaseContext()).load(s.getFile_src()).apply(options).into(imageView);
                }
            }

            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }

            @Override
            protected void onItemImageClick(Context context, List<ImageView> mImageViewList, ImageView imageView, int index, List<PostDetailBean.CiBean> list) {

                //点击查看大图功能
                GPreviewBuilder.from((Activity) context)
                        .setData(viewModel.getInfoList(list,mImageViewList))
                        .setCurrentIndex(index)
                        .setType(GPreviewBuilder.IndicatorType.Dot)
                        .start();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.setParams("circle_id",circle_id).setParams("cr_id_father","0").setParams("first_cr_id","0");
        viewModel.requestData(Constants.GET_CIRCLE_INFO);
        viewModel.requestData(Constants.GET_CIRCLE_REVIEWLIST);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.goVideo.observe(this, aVoid -> {
            Bundle bundle = new Bundle();
            bundle.putString("videoPath",viewModel.entity.get().getCi().get(0).getFile_src());
            bundle.putString("videoImgPath",viewModel.entity.get().getCircle_img());
            goToVideoPlayer(binding.videoBox,bundle);
        });

        viewModel.uc.goLookImg.observe(this,aVoid -> {
            UserViewInfo info = new UserViewInfo(viewModel.entity.get().getCircle_img());
            Rect bounds = new Rect();
            binding.videoImg.getGlobalVisibleRect(bounds);
            info.setBounds(bounds);

            //点击查看大图功能
            GPreviewBuilder.from(this)
                    .setSingleData(info)
                    .setCurrentIndex(0)
                    .setType(GPreviewBuilder.IndicatorType.Dot)
                    .start();
        });

        viewModel.uc.showEdit.observe(this, aVoid -> {
            InputMethodManager imm = (InputMethodManager) binding.editText.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                binding.editText.requestFocus();
                imm.showSoftInput(binding.editText, 0);
            }
            //binding.editText.requestFocus();
        });

        viewModel.uc.closeEdit.observe(this, aVoid -> {
            InputMethodManager imm = (InputMethodManager) binding.editText.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(binding.editText.getWindowToken(),0);
            }
        });

        viewModel.uc.sendMessage.observe(this,aVoid -> {
            if(!BaseUtil.isValue(binding.editText.getText().toString())){
                ToastUtils.showShort("请输入评论内容!");
                return;
            }
            viewModel.setParams("cr_content",binding.editText.getText().toString());
            viewModel.requestData(Constants.ADD_CIRCLE_REVIEW);
            binding.editText.setText("");
        });
    }

    @Override
    public void onKeyBoardStateChange(int state) {
        switch (state) {
            case KeyboardLayout.KEYBOARD_STATE_HIDE:
                binding.mengCheng.setVisibility(View.GONE);
                break;
            case KeyboardLayout.KEYBOARD_STATE_SHOW:
                binding.mengCheng.setVisibility(View.VISIBLE);
                binding.nScroll.scrollTo(0,binding.nScroll.getBottom());
                break;
        }
    }
}
